package com.design.classroommanagement.manager


import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.design.classroommanagement.BookmarkListFragment
import com.design.classroommanagement.admin.AdminHomeFragment
import com.design.classroommanagement.admin.AdminMyFragment
import com.design.classroommanagement.api.Role
import com.design.classroommanagement.api.User
import com.design.classroommanagement.api.dao.AdminDao
import com.design.classroommanagement.api.dao.StudentDao
import com.design.classroommanagement.api.dao.TeacherDao
import com.design.classroommanagement.databinding.ActivityMainBinding
import com.design.classroommanagement.student.StudentHomepage
import com.design.classroommanagement.teacher.TeacherHomeFragment
import com.design.classroommanagement.teacher.TeacherMyFragment
import com.design.classroommanagement.teacher.TeacherSearchFragment
import com.laughing.lib.utils.*

class BottomTabManager(var activity: FragmentActivity, var binding: ActivityMainBinding) {
    val user: User?
        get() = UserAccountManager.user

    val adminHomepage: AdminHomeFragment by lazy { AdminHomeFragment() }
    val adminMyFragment: AdminMyFragment by lazy { AdminMyFragment() }


    val teacherHomepage: TeacherHomeFragment by lazy { TeacherHomeFragment() }
    val teacherMyFragment: TeacherMyFragment by lazy { TeacherMyFragment() }
    val teacherSearchFragment: TeacherSearchFragment by lazy { TeacherSearchFragment() }
    val teacherClassTableFragment: BookmarkListFragment by lazy { BookmarkListFragment() }


    val studentHomepage: StudentHomepage by lazy { StudentHomepage() }

    init {
        when (user?.role) {
            Role.admin -> initAdmin()
            Role.teacher -> initTeacher()
            Role.student -> initStudent()
            else -> {}
        }
        binding.bottomLL.homeLL.performClick()
    }

    fun initAdmin() {
        Logs.d("initAdmin")
        binding.bottomLL.classTableLL.letGone()
        binding.bottomLL.searchLL.letGone()

        binding.bottomLL.apply {
            homeLL.clickWithTrigger {
                showFragment(adminHomepage)
                hideFragment(adminMyFragment)
            }

            meLL.clickWithTrigger {
                showFragment(adminMyFragment)
                hideFragment(adminHomepage)
            }

            searchLL.clickWithTrigger {
                runInBg {
                    try {
                        var list = AdminDao().query(){
                            put("id","1")
                        }
                        Logs.d("UserDao().query $list")
                        list = StudentDao().query()
                        Logs.d("StudentDao().query $list")
                        list = TeacherDao().query()
                        Logs.d("TeacherDao().query $list")

                        TeacherDao().add(User(account = "aauto2", password = "a"))

                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
            }
        }


    }

    fun initTeacher() {
        Logs.d("initTeacher")

        binding.bottomLL.apply {
            homeLL.clickWithTrigger {
                showFragment(teacherHomepage)
                hideFragment(teacherMyFragment)
                hideFragment(teacherSearchFragment)
                hideFragment(teacherClassTableFragment)
            }

            meLL.clickWithTrigger {
                showFragment(teacherMyFragment)
                hideFragment(teacherHomepage)
                hideFragment(teacherSearchFragment)
                hideFragment(teacherClassTableFragment)
            }

            searchLL.clickWithTrigger {
                hideFragment(teacherMyFragment)
                hideFragment(teacherHomepage)
                hideFragment(teacherClassTableFragment)
                showFragment(teacherSearchFragment)
            }
            classTableLL.clickWithTrigger {
                hideFragment(teacherMyFragment)
                hideFragment(teacherHomepage)
                showFragment(teacherClassTableFragment)
                hideFragment(teacherSearchFragment)
            }
        }
    }

    fun initStudent() {
        Logs.d("initStudent")

        binding.bottomLL.apply {
            homeLL.clickWithTrigger {
                showFragment(studentHomepage)
                hideFragment(adminMyFragment)
            }

            meLL.clickWithTrigger {
                showFragment(adminMyFragment)
                hideFragment(studentHomepage)
            }
        }
    }

    private fun showFragment(fragment: Fragment) {
        if (fragment.isAdded) {
            activity.supportFragmentManager.beginTransaction()
                .show(fragment).commitAllowingStateLoss()
        } else {
            activity.addFragment(fragment, binding.pageFl.id)
        }
    }


    private fun hideFragment(fragment: Fragment) {
        if (fragment.isAdded) {
            activity.supportFragmentManager.beginTransaction()
                .hide(fragment).commitAllowingStateLoss()
        } else {

        }
    }


}